﻿using System;
using System.Dynamic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using ReflexTérkép.Tests.dto;

namespace ReflexTérkép.Tests
{
    [TestClass]
    public class MappingToNullableTypesTests : TestBase
    {
        [TestMethod]
        public void Can_Map_DateTime_Values_To_Nullable_DateTime_Properties()
        {
            // Arrange
            const int id = 1;
            const string firstName = "Bob";
            const string lastName = "Smith";
            DateTime startDate = DateTime.Now.AddDays(-2);
            DateTime endDate = DateTime.Now; // This is what we are testing

            dynamic dynamicPerson = new ExpandoObject();
            dynamicPerson.Id = id;
            dynamicPerson.FirstName = firstName;
            dynamicPerson.LastName = lastName;
            dynamicPerson.StartDate = startDate;
            dynamicPerson.EndDate = endDate;

            // Act
            Employee employee = ReflexMapper.MapDynamic<Employee>(dynamicPerson);

            // Assert
            Assert.IsNotNull(employee);
            Assert.IsTrue(employee.Id == id);
            Assert.IsTrue(employee.FirstName == firstName);
            Assert.IsTrue(employee.LastName == lastName);
            Assert.IsTrue(employee.StartDate == startDate);
            Assert.IsTrue(employee.EndDate == endDate); // This is what we are testing
        }

        [TestMethod]
        public void Can_Map_Null_Values_To_Nullable_DateTime_Properties()
        {
            // Arrange
            const int id = 1;
            const string firstName = "Bob";
            const string lastName = "Smith";
            DateTime startDate = DateTime.Now.AddDays(-2);
            DateTime? endDate = null; // This is what we are testing

            dynamic dynamicPerson = new ExpandoObject();
            dynamicPerson.Id = id;
            dynamicPerson.FirstName = firstName;
            dynamicPerson.LastName = lastName;
            dynamicPerson.StartDate = startDate;
            dynamicPerson.EndDate = endDate;

            // Act
            Employee employee = ReflexMapper.MapDynamic<Employee>(dynamicPerson);

            // Assert
            Assert.IsNotNull(employee);
            Assert.IsTrue(employee.Id == id);
            Assert.IsTrue(employee.FirstName == firstName);
            Assert.IsTrue(employee.LastName == lastName);
            Assert.IsTrue(employee.StartDate == startDate);
            Assert.IsTrue(employee.EndDate == endDate); // This is what we are testing
        }

        [TestMethod]
        public void Can_Map_DateTime_String_Values_To_Nullable_DateTime_Properties()
        {
            // Arrange
            const int id = 1;
            const string firstName = "Bob";
            const string lastName = "Smith";
            DateTime startDate = DateTime.Now.AddDays(-2);
            DateTime? endDate = DateTime.Now;

            dynamic dynamicPerson = new ExpandoObject();
            dynamicPerson.Id = id;
            dynamicPerson.FirstName = firstName;
            dynamicPerson.LastName = lastName;
            dynamicPerson.StartDate = startDate.ToString();
            dynamicPerson.EndDate = endDate.ToString();

            // Act
            Employee employee = ReflexMapper.MapDynamic<Employee>(dynamicPerson);

            // Assert
            Assert.IsNotNull(employee);
            Assert.IsTrue(employee.Id == id);
            Assert.IsTrue(employee.FirstName == firstName);
            Assert.IsTrue(employee.LastName == lastName);
            Assert.IsTrue(employee.StartDate.ToString() == startDate.ToString());
            Assert.IsTrue(employee.EndDate.ToString() == endDate.ToString());

        }
    }
}
